package org.azj.sqlStatementGenerator;
import java.util.LinkedList;
import java.util.List;

import org.azj.logHelper.LOG;

// represent the brackets surrounding all single sql statement
// eg. ( A && B || C && D )
public class BracketStatement extends Statement {
	private List<Statement> statements;
	
	public BracketStatement() 
	{
		statements = new LinkedList<Statement>();
	}
	
	@Override
	public void addStatement(Statement statement)
			throws SqlStatementBuildingException {
		if (statements.contains(statement) == true) 
		{
			String statementString = "";
			try {
				statementString = statement.getStatements();
			} catch (Exception e) {
				LOG.WriteLog("数据格式不匹配");
				throw new SqlStatementBuildingTypeUnmatchedException(statementString);
			}
			throw new SqlStatementBuildingDuplicateException(statementString);
		}
		
		statements.add(statement);
	}

	@Override
	public void removeStatement(Statement statement)
			throws SqlStatementBuildingException {
		if (statements.contains(statement) == false)
		{
			String statementString = "";
			try {
				statementString = statement.getStatements();
			} catch (Exception e) {
				LOG.WriteLog("数据格式不匹配");
				throw new SqlStatementBuildingTypeUnmatchedException(statementString);
			}
			throw new SqlStatementBuildingNotExistException(statementString);
		}
		
		statements.remove(statement);
	}

	@Override
	public String getStatements()  throws SqlStatementBuildingException {
		String stm = "";
		if (statements.size() == 0) {
			return stm;
		}
		
		String statementString = "";
		try {
			for (Statement s : statements) {
				statementString = s.getStatements();
				stm += (statementString + " ");
			}	
		} catch (Exception e) {
			LOG.WriteLog("数据格式不匹配");
			throw new SqlStatementBuildingTypeUnmatchedException(statementString);
		}
		
		
		return "( " + stm + ")";
	}
}
